package com.xxjz.sc.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;

@Controller
public class HelloWorldController {

    Logger logger = LoggerFactory.getLogger("MiniFileController");

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        logger.info("==================");
        return "hello world";
    }

    public static void main(String[] args) {
        String url = "http://192.168.2.71:8181/bopcxf/userSyncController/syncAllUser";
        JSONObject o = new JSONObject();
        try {
            o.put("sysToken","m5nhFzvRVYRn/2PDTDEL2g==");

            String obj = o.toString();
            System.out.println(obj);
            System.out.println(postMethod(url,obj));

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //post
    public static String postMethod(String url, String query) throws IOException {
        URL restURL = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);
        PrintStream ps = new PrintStream(conn.getOutputStream());
        ps.print(query);
        ps.close();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        String result = "";
        while((line = br.readLine()) != null ){
            result += line;

//            System.out.print("[");
//            System.out.print(line);
//            System.out.print("]");
        }
        br.close();
        return result;
    }
}
